//
//  TalkPageViewController.m
//  SuperNews
//
//  Created by qiang on 16/1/9.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "TalkPageViewController.h"
#import "TalkTableViewCell.h"
#import "TalkModel.h"
#import "TalkDetailViewController.h"

@interface TalkPageViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) NSMutableArray *talkArr;

@end

//http://c.3g.163.com/newstopic/list/expert/0-10.html
//http://c.3g.163.com/newstopic/qa/EX7819202780533476264.html
//http://c.3g.163.com/newstopic/list/latestqa/EX7819202780533476264/0-10.html
//http://c.3g.163.com/newstopic/list/hotqa/EX7819202780533476264/0-10.html

@implementation TalkPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.number = 0;
    self.talkArr = [NSMutableArray array];
    
    [self createViews];
    [self getData];
    
    
    
}

- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/expert/%ld-10.html", self.number];
    [AFNetTool getNetWithURL:str body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"expertList"];
        [self.talkArr addObjectsFromArray: [TalkModel baseModelWithArr:arr]];
        self.number += 10;
        [self.tableV reloadData];
        [self.tableV.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (void)createViews
{
    self.navigationItem.title = @"问吧";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 54) style:UITableViewStyleGrouped];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[TalkTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    }];
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.talkArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TalkModel *talkModel = [self.talkArr objectAtIndex:indexPath.section];
    TalkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.imageV sd_setImageWithURL:(NSURL *)talkModel.picurl placeholderImage:[UIImage imageNamed:@"placehodler"]];
    [cell.imageH sd_setImageWithURL:(NSURL *)talkModel.headpicurl placeholderImage:[UIImage imageNamed:@"qipa"]];
    cell.nameLabel.text = talkModel.name;
    cell.detailLabel.text = talkModel.alias;
    cell.numberLabel.text = [NSString stringWithFormat:@"%@ 关注", talkModel.concernCount];
    cell.typeLabel.text = talkModel.classification;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TalkDetailViewController *detailVC = [[TalkDetailViewController alloc] init];
    detailVC.talkModel = [self.talkArr objectAtIndex:indexPath.section];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UIScreen mainScreen].bounds.size.height / 2.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
